package net.siufung.market.base.dept.controller.regulation;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.AllArgsConstructor;
import net.siufung.core.exception.BizServiceException;
import net.siufung.core.model.regulation.AbstractRegulation;
import net.siufung.market.base.dept.mapper.IDeptMapper;
import net.siufung.market.base.dept.mapper.entity.Dept;
import net.siufung.market.base.dept.api.IDeptUserApi;

import java.util.Objects;

/**
 * @author 陈建峰
 * @since 2022/7/31 10:49 下午
 */
@AllArgsConstructor
public class DeptRemoveRegulation extends AbstractRegulation<String> {

    private static final long serialVersionUID = 6524699392605387933L;

    private final IDeptMapper deptMapper;
    private final IDeptUserApi deptUserProvider;

    @Override
    public boolean isSatisfiedBy(String id) {
        if(deptMapper.selectCount(
                new LambdaQueryWrapper<Dept>().eq(Dept::getParentId, id)) > 0){
            throw new BizServiceException("部门存在子组织, 不能删除！");
        }
        if(Objects.nonNull(deptUserProvider) && deptUserProvider.existUsedDept(id)){
            throw new BizServiceException("部门存在用户，不能删除！");
        }
        return Boolean.TRUE;
    }
}
